package com.sanyu.lovefishing.ui.widgets.bottom_bar;

import android.graphics.Color;
import android.view.View;
import android.widget.RelativeLayout;

import com.lyf.core.ui.activity.BaseActivity;
import com.sanyu.lovefishing.R;
import com.sanyu.lovefishing.databinding.ActivityStickerBinding;
import com.sanyu.lovefishing.sticker.view.BubbleTextView;
import com.sanyu.lovefishing.sticker.view.StickerView;


public class StickerActivity extends BaseActivity<ActivityStickerBinding> {

    //当前处于编辑状态的贴纸
    private StickerView mCurrentView;
    //当前处于编辑状态的气泡
    private BubbleTextView mCurrentEditTextView;

    @Override
    protected ActivityStickerBinding getViewBinding() {
        return ActivityStickerBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void init() {
        super.init();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mViewBinding.btnAdd.setOnClickListener(view -> addStickerView());
        mViewBinding.btnAdd2.setOnClickListener(view -> addBubble());
    }

    //添加表情
    private void addStickerView() {
        final StickerView stickerView = new StickerView(this);
        stickerView.setImageResource(R.mipmap.ic_cat);
        stickerView.setOperationListener(new StickerView.OperationListener() {
            @Override
            public void onDeleteClick() {
//                mViews.remove(stickerView);
                mViewBinding.rlContentRoot.removeView(stickerView);
            }

            @Override
            public void onEdit(StickerView stickerView) {
                if (mCurrentEditTextView != null) {
                    mCurrentEditTextView.setInEdit(false);
                }
                mCurrentView.setInEdit(false);
                mCurrentView = stickerView;
                mCurrentView.setInEdit(true);
            }

            @Override
            public void onTop(StickerView stickerView) {
//                int position = mViews.indexOf(stickerView);
//                if (position == mViews.size() - 1) {
//                    return;
//                }
//                StickerView stickerTemp = (StickerView) mViews.remove(position);
//                mViews.add(mViews.size(), stickerTemp);
            }
        });
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        mViewBinding.rlContentRoot.addView(stickerView, lp);
        setCurrentEdit(stickerView);
    }




    //添加气泡
    private void addBubble() {
        final BubbleTextView bubbleTextView = new BubbleTextView(this,
                Color.WHITE, 0);
        bubbleTextView.setImageResource(R.mipmap.bubble_7_rb);
        bubbleTextView.setOperationListener(new BubbleTextView.OperationListener() {
            @Override
            public void onDeleteClick() {
                mViewBinding.rlContentRoot.removeView(bubbleTextView);
            }

            @Override
            public void onEdit(BubbleTextView bubbleTextView) {
                if (mCurrentView != null) {
                    mCurrentView.setInEdit(false);
                }
                mCurrentEditTextView.setInEdit(false);
                mCurrentEditTextView = bubbleTextView;
                mCurrentEditTextView.setInEdit(true);
            }

            @Override
            public void onClick(BubbleTextView bubbleTextView) {

            }

            @Override
            public void onTop(BubbleTextView bubbleTextView) {
            }
        });
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        mViewBinding.rlContentRoot.addView(bubbleTextView, lp);
        setCurrentEdit(bubbleTextView);
    }



    /**
     * 设置当前处于编辑模式的贴纸
     */
    private void setCurrentEdit(StickerView stickerView) {
        if (mCurrentView != null) {
            mCurrentView.setInEdit(false);
        }
        if (mCurrentEditTextView != null) {
            mCurrentEditTextView.setInEdit(false);
        }
        mCurrentView = stickerView;
        stickerView.setInEdit(true);
    }

    /**
     * 设置当前处于编辑模式的气泡
     */
    private void setCurrentEdit(BubbleTextView bubbleTextView) {
        if (mCurrentView != null) {
            mCurrentView.setInEdit(false);
        }
        if (mCurrentEditTextView != null) {
            mCurrentEditTextView.setInEdit(false);
        }
        mCurrentEditTextView = bubbleTextView;
        mCurrentEditTextView.setInEdit(true);
    }

}
